// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "merchant_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "所属商户",
    "label": "所属商户"
  },
  "type_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "所属分类",
    "label": "所属分类"
  },
  "product_name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "minLength": 2,
        "maxLength": 20
      }
    ],
    "title": "商品名称",
    "label": "商品名称"
  },
  "product_base_price": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "double"
      }
    ],
    "title": "商品基础价格",
    "label": "商品基础价格"
  },
  "product_discount": {
    "rules": [
      {
        "format": "double"
      }
    ],
    "title": "商品打折",
    "label": "商品打折"
  },
  "product_icon": {
    "rules": [
      {
        "format": "array"
      },
      {
        "arrayType": "file"
      },
      {
        "maxLength": 1
      }
    ],
    "title": "商品图片",
    "label": "商品图片"
  },
  "product_inventory": {
    "rules": [
      {
        "format": "array"
      },
      {
        "arrayType": "object"
      }
    ],
    "title": "价格库存信息",
    "label": "价格库存信息"
  },
  "product_status": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "正在热销",
            "value": 0
          },
          {
            "text": "暂停停售",
            "value": 1
          },
          {
            "text": "样品展示",
            "value": 2
          }
        ]
      }
    ],
    "title": "商品状态",
    "defaultValue": 0,
    "label": "商品状态"
  },
  "product_lable": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "普通",
            "value": 0
          },
          {
            "text": "新品",
            "value": 1
          },
          {
            "text": "招牌",
            "value": 2
          }
        ]
      }
    ],
    "title": "商品标签",
    "defaultValue": 0,
    "label": "商品标签"
  },
  "product_sort": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "商品排行",
    "defaultValue": 1,
    "label": "商品排行"
  },
  "product_description": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "商品描述",
    "label": "商品描述"
  }
}

const enumConverter = {
  "product_status_valuetotext": {
    "0": "正在热销",
    "1": "暂停停售",
    "2": "样品展示"
  },
  "product_lable_valuetotext": {
    "0": "普通",
    "1": "新品",
    "2": "招牌"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
